package com.xian.hadoop.io;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * hadoop压缩与解压
 */
public class TestCompress {
    public static void main(String[] args) throws Exception {
        TestCompress testCompress = new TestCompress();

        testCompress.compress(); //压缩


        testCompress.decompress(); //解压

    }

    /**
     * 压缩
     */
    public void compress() throws Exception {
        String fileName = "E:\\temp\\1_Hadoop HA原理.mp4";

        //压缩
        FileInputStream fis = new FileInputStream(new File(fileName));
        Class codecClass = Class.forName("org.apache.hadoop.io.compress.BZip2Codec"); //指定压缩格式
        CompressionCodec codec = (CompressionCodec)ReflectionUtils.newInstance(codecClass, new Configuration());
        //输出流
        String compressFile = fileName + codec.getDefaultExtension(); //压缩文件名(原文件名加压缩格式后缀)
        FileOutputStream fos = new FileOutputStream(new File(compressFile));
        CompressionOutputStream cos = codec.createOutputStream(fos);

        //拷贝流
        IOUtils.copyBytes(fis, cos, 1024*1024*10);

        //关闭资源
        cos.close();
        fos.close();
        fis.close();


    }


    /**
     * 解压缩
     */
    public void decompress() throws Exception {
        String compressFile = "E:\\temp\\1_Hadoop HA原理.mp4.bz2";



        //解压
        //检验是否可解压
        CompressionCodecFactory codecFactory = new CompressionCodecFactory(new Configuration());
        CompressionCodec codec = codecFactory.getCodec(new Path(compressFile));
        // （1）获取输入流
        CompressionInputStream cis = codec.createInputStream(new FileInputStream(new File(compressFile)));

        // （2）获取输出流
        FileOutputStream fos = new FileOutputStream(new File(compressFile + ".decoded"));

        // （3）流的对拷
        IOUtils.copyBytes(cis, fos, 1024*1024*5, false);

        // （4）关闭资源
        cis.close();
        fos.close();
    }


}
